package com.zpl.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpl.order.entity.Order;
import com.zpl.order.entity.dto.OrderAllKeyWordDTO;
import com.zpl.order.entity.dto.OrderKeyWordDTO;
import com.zpl.order.entity.vo.OrderAllVO;
import com.zpl.order.enums.OrderStatus;
import com.zpl.order.mapper.OrderMapper;
import com.zpl.order.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public boolean insert(Order order) {
        if(orderMapper.insert(order)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Long id) {
        if(orderMapper.deleteById(id)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Order order) {
        if(orderMapper.updateById(order)>0){
            return true;
        }
        return false;
    }

    @Override
    public Order selectById(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public IPage<Order> selectByMemberId(OrderKeyWordDTO orderKeyWordDTO) {
        Page page = new Page<>(orderKeyWordDTO.getCurrentPage(), orderKeyWordDTO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        // 条件
        if (!StringUtils.isEmpty(orderKeyWordDTO.getMemberId())) {
            System.out.println(JSON.toJSON(orderKeyWordDTO));
            queryWrapper.eq("member_id", orderKeyWordDTO.getMemberId());
            if(!StringUtils.isEmpty(orderKeyWordDTO.getOrderStatus())){
                queryWrapper.eq("order_status",orderKeyWordDTO.getOrderStatus());
                if (orderKeyWordDTO.getDay()!=0){
                    System.out.println("2");
                    orderKeyWordDTO.setCurrentPage((orderKeyWordDTO.getCurrentPage() - 1) * orderKeyWordDTO.getPageSize());
                    List<Order> list = orderMapper.getOrderByDayandStatus(orderKeyWordDTO);
                    page.setRecords(list);
                    page.setCurrent(orderKeyWordDTO.getCurrentPage());
                    page.setSize(orderKeyWordDTO.getPageSize());
                    int count = orderMapper.getOrderTotal(orderKeyWordDTO);
                    page.setTotal(count);
                    return page;
                }
            }else if(orderKeyWordDTO.getDay()!=0){
                System.out.println("3");
                orderKeyWordDTO.setCurrentPage((orderKeyWordDTO.getCurrentPage() - 1) * orderKeyWordDTO.getPageSize());
                List<Order> list = orderMapper.getOrderByDayandStatus(orderKeyWordDTO);
                page.setRecords(list);
                page.setCurrent(orderKeyWordDTO.getCurrentPage());
                page.setSize(orderKeyWordDTO.getPageSize());
                int count = orderMapper.getOrderTotal(orderKeyWordDTO);
                page.setTotal(count);
                return page;
            }
            System.out.println("1");
            queryWrapper.eq("delete_status","0");
            IPage<Order> orderIPage = orderMapper.selectPage(page, queryWrapper);
            return orderIPage;
        }else {
            return null;
        }
    }

    @Override
    public IPage<OrderAllVO> getOrderAllList(OrderAllKeyWordDTO orderAllKeyWordDTO) {
        Page<OrderAllVO> page = new Page<>();
        orderAllKeyWordDTO.setCurrentPage((orderAllKeyWordDTO.getCurrentPage() - 1) * orderAllKeyWordDTO.getPageSize());
        List<OrderAllVO> list = orderMapper.getOrderAllList(orderAllKeyWordDTO);
//        List<OrderAllVO> orderAllVOList = new ArrayList<>();
//        for (OrderAllVO orderAllVO : list) {
//            // 进行一个转换，将我们的code 转为name
//            orderAllVO.setOrderStatus(OrderStatus.getNameByCode(orderAllVO.getDepartment()));
//            orderAllVO.setJob(Job.getNameByCode(orderAllVO.getJob()));
//            orderAllVO.setLevel(Level.getNameByCode(orderAllVO.getLevel()));
//            orderAllVOList.add(orderAllVO);
//        }

        page.setRecords(list);
        page.setCurrent(orderAllKeyWordDTO.getCurrentPage());
        page.setSize(orderAllKeyWordDTO.getPageSize());

        int count = orderMapper.getOrderAllTotal(orderAllKeyWordDTO);
        page.setTotal(count);
        return page;
    }

}
